/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cater.common.utils;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.cater.common.SpringContextHolder;
import com.cater.sys.dao.DictionarydataDao;
import com.cater.sys.entity.Dictionarydata;
import com.google.common.collect.Lists;

/**
 * 字典工具类
 * 
 * @author ThinkGem
 * @version 2013-5-29
 */
@Service
public class DictUtils {

	private static DictionarydataDao dao = SpringContextHolder.getBean(DictionarydataDao.class);

	public static String getDictLabel(String value, String type, String defaultValue) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
			for (Dictionarydata dict : getDictList(type)) {
				if (type.equals(dict.getType()) && value.equals(dict.getValue())) {
					return dict.getValueName();
				}
			}
		}
		return defaultValue;
	}

	public static String getDictLabels(String values, String type, String defaultValue) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)) {
			List<String> valueList = Lists.newArrayList();
			for (String value : StringUtils.split(values, ",")) {
				valueList.add(getDictLabel(value, type, defaultValue));
			}
			return StringUtils.join(valueList, ",");
		}
		return defaultValue;
	}

	public static String getDictValue(String label, String type, String defaultLabel) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)) {
			for (Dictionarydata dict : getDictList(type)) {
				if (type.equals(dict.getType()) && label.equals(dict.getValueName())) {
					return dict.getValue();
				}
			}
		}
		return defaultLabel;
	}

	public static List<Dictionarydata> getDictList(String type) {
		List<Dictionarydata> dictList = dao.getDataByType(type);
		return dictList;
	}

}
